package org.example.util;


import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 密码加密工具类
 * 使用BCrypt算法进行密码加密和验证
 */
@Component
public class PasswordEncoder {
    
    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    
    /**
     * 加密密码
     * @param rawPassword 原始密码
     * @return 加密后的密码
     */
    public static String encode(String rawPassword) {
        return encoder.encode(rawPassword);
    }
    
    /**
     * 验证密码
     * @param rawPassword 原始密码
     * @param encodedPassword 加密后的密码
     * @return 是否匹配
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        return encoder.matches(rawPassword, encodedPassword);
    }
    
    /**
     * 生成加密密码示例
     */
    public static void main(String[] args) {
        String password1 = "123456";
        String password2 = "admin123";
        
        System.out.println("=== 密码加密示例 ===");
        System.out.println("原始密码: " + password1);
        System.out.println("加密后: " + encode(password1));
        System.out.println();
        System.out.println("原始密码: " + password2);
        System.out.println("加密后: " + encode(password2));
        System.out.println();
        
        // 验证示例
        String encoded1 = encode(password1);
        System.out.println("验证密码 '" + password1 + "': " + matches(password1, encoded1));
        System.out.println("验证错误密码: " + matches("wrong", encoded1));
    }
} 